package com.gxa.xiaoucomeshome.service.impl;

import com.gxa.xiaoucomeshome.exception.BusinessException;
import com.gxa.xiaoucomeshome.exception.ExceptionCode;
import com.gxa.xiaoucomeshome.mapper.CourierMapper;
import com.gxa.xiaoucomeshome.pojo.Courier;
import com.gxa.xiaoucomeshome.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 万骞
 * @date 2022-06-07
 */

@Service("courierService")
@Transactional(rollbackFor = Throwable.class)
public class CourierServiceImpl implements CourierService {

    @Autowired
    CourierMapper courierMapper;

    @Override
    public Courier login(String phone, String password) throws Exception {
        Courier courier = courierMapper.login(phone, password);
        if (courier == null) {
            throw new BusinessException(ExceptionCode.COURIER_NOT_FOUND);
        }
        return courier;
    }

    @Override
    public Map<String, Object> selectByPhone(String phone) {
        return courierMapper.selectByPhone(phone);
    }

    @Override
    public List<Map<String, Object>> selectBySiteName(String siteName) throws Exception {
        List<Map<String, Object>> list = courierMapper.selectBySiteName(siteName);
        if (!list.isEmpty()) {
            return list;
        } else {
            throw new BusinessException(ExceptionCode.COURIER_SELECT_FAILED);
        }
    }

    @Override
    public Map<String, Object> selectPendingPickup(String phone) throws Exception {
        Map<String, Object> map = courierMapper.selectPendingPickup(phone);
        if (!map.isEmpty()) {
            return map;
        } else {
            throw new BusinessException(ExceptionCode.COURIER_SELECT_FAILED);
        }
    }

    @Override
    public Map<String, Object> selectPendingDelivery(String phone) throws Exception {
        Map<String, Object> map = courierMapper.selectPendingDelivery(phone);

        if (map == null) {
            throw new BusinessException(ExceptionCode.COURIER_SELECT_FAILED);
        }
        return map;
    }

    @Override
    public Map<String, Object> selectSignedSuccessfully(String phone) throws Exception {
        Map<String, Object> map = courierMapper.selectSignedSuccessfully(phone);
        if (!map.isEmpty()) {
            return map;
        } else {
            throw new BusinessException(ExceptionCode.COURIER_SELECT_FAILED);
        }
    }

    @Override
    public Map<String, Object> selectFailedToSign(String phone) throws Exception {
        Map<String, Object> map = courierMapper.selectFailedToSign(phone);
        if (!map.isEmpty()) {
            return map;
        } else {
            throw new BusinessException(ExceptionCode.COURIER_SELECT_FAILED);
        }
    }

    @Override
    public void updatePassword(String phone, String oldPassword, String newPassword, String newPassword2) throws Exception {
        if (oldPassword.isEmpty() || newPassword.isEmpty()) {
            throw new BusinessException(ExceptionCode.PASSWORD_NOT_ISEMPTY);
        } else {
            if (courierMapper.selectPassword(phone).equals(oldPassword) &&
                    Objects.equals(newPassword, newPassword2)) {
                courierMapper.updatePassword(phone,oldPassword, newPassword);
            } else {
                throw new BusinessException(ExceptionCode.COURIER_UPDATE_FAILED);
            }
        }
    }

    @Override
    public void updateForGrabOrders(String phone, String id) throws Exception {
        Integer integer = courierMapper.updateForGrabOrders(phone, id);
        if (integer == 1) {
            courierMapper.updateDispatchTime(id);
        } else {
            throw new BusinessException(ExceptionCode.COURIER_GRABORDER_FAILED);
        }
    }

    @Override
    public List<Map<String, Object>> getListPendingPickUp(String phone) throws Exception {
        List<Map<String, Object>> listPendingPickUp = courierMapper.getListPendingPickUp(phone);
        if (listPendingPickUp == null) {
            throw new BusinessException(ExceptionCode.ORDER_NOT_FOUND);
        } else {
            return listPendingPickUp;
        }
    }

    @Override
    public List<Map<String, Object>> getListPendingDelivery(String phone) throws Exception {
        List<Map<String, Object>> listPendingDelivery = courierMapper.getListPendingDelivery(phone);
        if (listPendingDelivery == null) {
            throw new BusinessException(ExceptionCode.ORDER_NOT_FOUND);
        } else {
            return listPendingDelivery;
        }
    }

    @Override
    public List<Map<String, Object>> getListSignInSuccess(String phone) throws Exception {
        List<Map<String, Object>> listSignInSuccess = courierMapper.getListSignInSuccess(phone);
        if (listSignInSuccess == null) {
            throw new BusinessException(ExceptionCode.ORDER_NOT_FOUND);
        } else {
            return listSignInSuccess;
        }
    }

    @Override
    public List<Map<String, Object>> getListSignInFailed(String phone) throws Exception {
        List<Map<String, Object>> listSignInFailed = courierMapper.getListSignInFailed(phone);
        if (listSignInFailed == null) {
            throw new BusinessException(ExceptionCode.ORDER_NOT_FOUND);
        } else {
            return listSignInFailed;
        }

    }

    @Override
    public Map<String, Object> pendingPickUpDetail(String id) throws Exception {
        Map<String, Object> stringObjectMap = courierMapper.pendingPickUpDetail(id);
        if (stringObjectMap == null) {
            throw new BusinessException(ExceptionCode.ORDERDETAIL_NOT_FOUND);
        } else {
            return stringObjectMap;
        }
    }

    @Override
    public Map<String, Object> pendingDeliveryDetail(String id) throws Exception {
        Map<String, Object> stringObjectMap = courierMapper.pendingDeliveryDetail(id);
        if (stringObjectMap == null) {
            throw new BusinessException(ExceptionCode.ORDERDETAIL_NOT_FOUND);
        } else {
            return stringObjectMap;
        }
    }

    @Override
    public Map<String, Object> signInSuccessDetail(String id) throws Exception {
        Map<String, Object> stringObjectMap = courierMapper.signInSuccessDetail(id);
        if (stringObjectMap == null) {
            throw new BusinessException(ExceptionCode.ORDERDETAIL_NOT_FOUND);
        } else {
            return stringObjectMap;
        }
    }

    @Override
    public Map<String, Object> signInFailedDetail(String id) throws Exception {
        Map<String, Object> stringObjectMap = courierMapper.signInFailedDetail(id);
        if (stringObjectMap == null) {
            throw new BusinessException(ExceptionCode.ORDERDETAIL_NOT_FOUND);
        } else {
            return stringObjectMap;
        }
    }

    @Override
    public boolean pickUp(String id) throws BusinessException {
        Integer integer = courierMapper.updateStatePickUp(id);
        Integer integer1 = courierMapper.updateTimePickUp(id);
        if (integer == 0 || integer1 == 0) {
            throw new BusinessException(ExceptionCode.ORDERSTATE_UPDATE_FAILD);
        } else if (integer == 1 && integer1 == 1) {
            return true;
        } else {
            throw new BusinessException(ExceptionCode.SERVER_FAILED);
        }
    }

    @Override
    public boolean signIn(boolean signIn, String desc, String remark, String url1,String url2, String id) throws Exception {

        Integer integer;
        Integer integer1 = courierMapper.updateTimeSignIn(id);
        Integer integer2;
        if (signIn) {
            //签收成功
            integer= courierMapper.updateStateSignInSuccess(id);
            integer2 = courierMapper.updateDetailSignInSuccess(desc, remark, url1,url2 ,id);
        } else {
            //签收失败
            integer= courierMapper.updateStateSignInFailed(id);
            integer2 = courierMapper.updateDetailSignInFailed(desc, url1,url2, id);
            //退回状态设置为0未退回
            courierMapper.updateStateSendBack(id, false);
        }
        if (integer == 0 || integer1 == 0 || integer2 == 0) {
            throw new BusinessException(ExceptionCode.ORDERSTATE_UPDATE_FAILD);
        } else if (integer == 1 && integer1 == 1 && integer2 == 1) {
            return true;
        } else {
            throw new BusinessException(ExceptionCode.SERVER_FAILED);
        }
    }

    @Override
    public boolean sendBack(String reason, String id) throws Exception {
        Integer integer = courierMapper.updateSendBack(reason, id);
        if (integer == 0) {
            throw new BusinessException(ExceptionCode.ORDERSTATE_UPDATE_FAILD);
        } else {
            return true;
        }
    }
}
